package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.dubbo.utils.TimeLineService;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementTimeLine;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@DubboService
public class MovementApiImpl implements MovementApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TimeLineService timeLineService;

    // 发布动态
    @Override
    public void publish(Movement movement) {

        try {
            // 设置pid
            movement.setPid(idWorker.getNextId("movement"));

            //设置时间
            movement.setCreated(System.currentTimeMillis());

            mongoTemplate.save(movement);

            timeLineService.saveTimeLine(movement.getUserId(),movement.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //查询 个人动态
    @Override
    public PageResult findByUserId(Long userId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Movement> movements = mongoTemplate.find(query, Movement.class);
        return new PageResult(page,pagesize,0L,movements);
    }

    //查询好友动态
    @Override
    public List<Movement> findFriendMovements(Long freindId, Integer page, Integer pagesize) {
        // 1 查询好友 时间线表
        Query query = Query.query(Criteria.where("friendId").in(freindId)).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));

        List<MovementTimeLine> lines = mongoTemplate.find(query, MovementTimeLine.class);
        // 2 提取动态id 集合
        List<ObjectId> movementIds = CollUtil.getFieldValues(lines, "movementId", ObjectId.class);

        // 3 根据动态id 查询动态详情
        Query movementQuery = Query.query(Criteria.where("id").in(movementIds)).with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(movementQuery,Movement.class);
    }

    //随机获取动态集合
    @Override
    public List<Movement> randomMovements(Integer pageseze) {
        TypedAggregation<Movement> aggregation = Aggregation.newAggregation(Movement.class, Aggregation.sample(pageseze));
        AggregationResults<Movement> movements = mongoTemplate.aggregate(aggregation, Movement.class);
        return movements.getMappedResults();
    }

    //根据pid 查询
    @Override
    public List<Movement> findByPids(List<Long> pids) {
        Query query = Query.query(Criteria.where("pid").in(pids));
        return mongoTemplate.find(query,Movement.class);
    }

    // 根据id 查询
    @Override
    public Movement findById(String movementId) {
        return mongoTemplate.findById(movementId,Movement.class);
    }

}